//
//  IsLogout.m
//  SoundPill
//
//  Created by 宋旭东 on 15/7/1.
//  Copyright (c) 2015年 宋旭东. All rights reserved.
//

#import "IsLogout.h"
#import "AFNetworking.h"
#import "My_ViewController.h"
@implementation IsLogout
+ (void)userIsLogout:(PlazzViewController *)controller Alert:(BOOL)alert
{
    if ([[NSUserDefaults standardUserDefaults] valueForKey:@"uid"] != nil) {
        NSString *url = @"http://123.57.1.143/api/isloginscore";
        NSString *requestheader = [NSString stringWithFormat:@"Token %@:%@",[[NSUserDefaults standardUserDefaults] valueForKey:@"uid"], [[NSUserDefaults standardUserDefaults] valueForKey:@"token"]];
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", @"text/plain", nil];
        [manager.requestSerializer setValue:requestheader forHTTPHeaderField:@"authorization"];
        [manager GET:url parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSLog(@"-------/n%@/n",responseObject);
            if (alert == YES) {
                if ([[responseObject valueForKey:@"state"] isEqualToString:@"success"]) {
                    if ([[responseObject valueForKey:@"sign"] isEqualToString:@"success"]) {
                        [controller qianDaoAlert:responseObject showSignBtn:YES];
                    } else {
                        [controller qianDaoAlert:responseObject showSignBtn:NO];
                    }
                } else {
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"logout" object:nil];
                }
            } else {
                if ([[responseObject valueForKey:@"state"] isEqualToString:@"success"]) {
                    if ([[responseObject valueForKey:@"sign"] isEqualToString:@"success"]) {
                        [controller qianDaoAlert:responseObject showSignBtn:YES];
                    }
                } else {
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"logout" object:nil];
                }
            }
            if ([[responseObject valueForKey:@"systemmessages"] integerValue] > 0) {
                NSString *str = [responseObject valueForKey:@"systemmessages"];
                [[NSNotificationCenter defaultCenter] postNotificationName:@"checkUnreadMessageCount" object:str];
            }
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
        }];
    }
    
}
@end
